package com.daily.DailyStudy.动态代理.JDK原生动态代理.interceptor;


import com.daily.DailyStudy.动态代理.JDK原生动态代理.entity.UserProxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class UserServiceInterceptor implements InvocationHandler {

    private Object realObj;

    public Object getRealObj() {
        return realObj;
    }

    public void setRealObj(Object realObj) {
        this.realObj = realObj;
    }

    public UserServiceInterceptor(Object realObj) {
        super();
        this.realObj = realObj;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if( args!=null && args.length>0 && args[0] instanceof UserProxy){
            UserProxy user = (UserProxy) args[0];
            if(user.getName().trim().length()<=1){
                throw new RuntimeException("用户姓名输入长度需要大于1！");
            }
        }
        Object ret = method.invoke(realObj, args);
        System.out.println("UserServiceInterceptor:::数据库操作成功！");
        return ret;
    }
}
